using UnityEngine;
#if UNITY_EDITOR
using System;
using UnityEditor;
using System.Reflection;
using System.Text.RegularExpressions;
#endif

/// <summary>
/// 重命名属性
/// </summary>

#if UNITY_EDITOR
[AttributeUsage(AttributeTargets.Field)]
#endif
public class RenameAttribute : PropertyAttribute {
    public string name = "";                                        // 枚举名称
    public string htmlColor = "#B3B3B3";                            // 文本颜色

    /// <summary>
    /// 重命名属性
    /// </summary>
    /// <param name="name"></param>
    public RenameAttribute(string name) {
        this.name = name;
    }

    /// <summary>
    /// 重命名属性
    /// </summary>
    /// <param name="name">新名称</param>
    /// <param name="htmlColor">文本颜色 例如："#FFFFFF" 或 "black"</param>
    public RenameAttribute(string name, string htmlColor) {
        this.name = name;
        this.htmlColor = htmlColor;
    }
}

#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(RenameAttribute))]
public class RenameDrawer : PropertyDrawer {
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
        RenameAttribute rename = (RenameAttribute)attribute;        // 替换属性名称
        label.text = rename.name;

        // 重绘GUI
        Color defaultColor = EditorStyles.label.normal.textColor;
        EditorStyles.label.normal.textColor = htmlToColor(rename.htmlColor);
        bool isElement = Regex.IsMatch(property.displayName, "Element \\d+");
        if (isElement) label.text = property.displayName;
        if (property.propertyType == SerializedPropertyType.Enum) {
            drawEnum(position, property, label);
        } else {
            EditorGUI.PropertyField(position, property, label, true);
        }
        EditorStyles.label.normal.textColor = defaultColor;
    }

    // 绘制枚举类型
    private void drawEnum(Rect position, SerializedProperty property, GUIContent label) {
        EditorGUI.BeginChangeCheck();

        // 获取枚举相关属性
        Type type = fieldInfo.FieldType;
        string[] names = property.enumNames;
        string[] values = new string[names.Length];
        Array.Copy(names, values, names.Length);
        while (type.IsArray) type = type.GetElementType();

        // 获取枚举所对应的RenameAttribute
        for (int i = 0; i < names.Length; i++) {
            FieldInfo info = type.GetField(names[i]);
            RenameAttribute[] atts = (RenameAttribute[])info.GetCustomAttributes(typeof(RenameAttribute), true);
            if (atts.Length != 0) values[i] = atts[0].name;
        }

        // 重绘GUI
        int index = EditorGUI.Popup(position, label.text, property.enumValueIndex, values);
        if (EditorGUI.EndChangeCheck() && index != -1) property.enumValueIndex = index;
    }

    /// <summary>
    /// Html颜色转换为Color
    /// </summary>
    /// <param name="hex"></param>
    /// <returns></returns>
    public static Color htmlToColor(string hex) {
        // 编辑器默认颜色
        Color result = new Color(0.705f, 0.705f, 0.705f);
        if (string.IsNullOrEmpty(hex)) return result;

        // 转换颜色
        hex = hex.ToLower();
        if (hex.IndexOf("#") == 0 && hex.Length == 7) {
            int r = Convert.ToInt32(hex.Substring(1, 2), 16);
            int g = Convert.ToInt32(hex.Substring(3, 2), 16);
            int b = Convert.ToInt32(hex.Substring(5, 2), 16);
            result = new Color(r / 255f, g / 255f, b / 255f);
            return result;
        };
        switch (hex){
            case "red":
                result = Color.red;
            break;
            case "green":
                result = Color.green;
            break;
            case "blue":
                result = Color.blue;
            break;
            case "yellow":
                result = Color.yellow;
            break;
            case "black":
                result = Color.black;
            break;
            case "white":
                result = Color.white;
            break;
            case "cyan":
                result = Color.cyan;
            break;
            case "gray":
                result = Color.gray;
            break;
            case "grey":
                result = Color.grey;
            break;
            case "magenta":
                result = Color.magenta;
            break;
            case "orange":
                result = new Color(1, 165 / 255f, 0);
            break;
            default:
            break;
        }
        return result;
    }
}
#endif

/// <summary>
/// 添加标题属性
/// </summary>
#if UNITY_EDITOR
[AttributeUsage(AttributeTargets.Field)]
#endif
public class TitleAttribute : PropertyAttribute {

    public string title = "";                                       // 标题名称
    public string htmlColor = "#B3B3B3";                            // 文本颜色

    /// <summary>
    /// 在属性上方添加一个标题
    /// </summary>
    /// <param name="title">标题名称</param>
    public TitleAttribute(string title) {
        this.title = title;
    }

    /// <summary>
    /// 在属性上方添加一个标题
    /// </summary>
    /// <param name="title">标题名称</param>
    /// <param name="htmlColor">文本颜色 例如："#FFFFFF" 或 "black"</param>
    public TitleAttribute(string title, string htmlColor) {
        this.title = title;
        this.htmlColor = htmlColor;
    }
}

#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(TitleAttribute))]
public class TitleAttributeDrawer : DecoratorDrawer {
    private GUIStyle style = new GUIStyle(EditorStyles.label);      // 文本样式
    public override void OnGUI(Rect position) {
        // 获取Attribute
        TitleAttribute rename = (TitleAttribute)attribute;
        style.fixedHeight = 18;
        style.normal.textColor = RenameDrawer.htmlToColor(rename.htmlColor);

        // 重绘GUI
        position = EditorGUI.IndentedRect(position);
        GUI.Label(position, rename.title, style);
    }

    public override float GetHeight() {
        return base.GetHeight() - 3;
    }

}
#endif

/// <summary>
/// 重命名脚本编辑器中的属性名称
/// </summary>
#if UNITY_EDITOR
[AttributeUsage(AttributeTargets.Field)]
#endif
public class RenameInEditorAttribute : PropertyAttribute {
    public string name = "";                                    // 新名称

    /// <summary>
    /// 重命名属性
    /// </summary>
    /// <param name="name">新名称</param>
    public RenameInEditorAttribute(string name) {
        this.name = name;
    }
}